package com.ht.module.bus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.abnormal.HtException;
import com.ht.config.dataAuth.DataScope;
import com.ht.constant.BusConstant;
import com.ht.constant.DicConstants;
import com.ht.module.bus.entity.TClass;
import com.ht.module.bus.entity.TDistrict;
import com.ht.module.bus.entity.TTransportationCompanyInformation;
import com.ht.module.bus.entity.TTransportationCompanyStatistics;
import com.ht.module.bus.mapper.TClassMapper;
import com.ht.module.bus.mapper.TTransportationCompanyStatisticsMapper;
import com.ht.module.bus.service.TClassService;
import com.ht.module.bus.service.TDistrictService;
import com.ht.module.bus.service.TTransportationCompanyInformationService;
import com.ht.module.bus.service.TTransportationCompanyStatisticsService;
import com.ht.module.sys.entity.SysDictItem;
import com.ht.module.sys.service.ISysDictService;
import com.ht.util.CommMethod;
import com.ht.util.Pager;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.time.Year;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hejialun
 * @since 2024-07-29
 */
@Service
public class TTransportationCompanyStatisticsServiceImpl extends ServiceImpl<TTransportationCompanyStatisticsMapper, TTransportationCompanyStatistics> implements TTransportationCompanyStatisticsService {
    @Resource
    private TDistrictService districtService;
    @Resource
    private TTransportationCompanyInformationService transportationCompanyInformationService;

    @Override
    public Pager<TTransportationCompanyStatistics> findPage(Pager<TTransportationCompanyStatistics> pager, TTransportationCompanyStatistics en) {
        return baseMapper.findPageAuth(pager, getQW(en),null);
    }

    private QueryWrapper<TTransportationCompanyStatistics> getQW(TTransportationCompanyStatistics en) {
        QueryWrapper<TTransportationCompanyStatistics> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(en.getCompanyName())) {
            qw.like("company_name", en.getCompanyName());
        }

        if (StrUtil.isNotEmpty(en.getCompanyCode())) {
            qw.eq("company_code", en.getCompanyCode());
        }

        if (StrUtil.isNotEmpty(en.getDistrictName())) {
            qw.like("district_name", en.getDistrictName());
        }

        if (StrUtil.isNotEmpty(en.getDistrictCode())) {
            qw.eq("district_code", en.getDistrictCode());
        }

        if (StrUtil.isNotEmpty(en.getEducationStage())) {
            qw.eq("education_stage", en.getEducationStage());
        }

        if (StrUtil.isNotEmpty(en.getState())) {
            qw.eq("state", en.getState());
        }
        qw.eq("del_flag", BusConstant.DEL_FLAG_N);
        return qw;
    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            List<TTransportationCompanyStatistics> list = EasyExcel.read(file.getInputStream())
                    .head(TTransportationCompanyStatistics.class)
                    .sheet()
                    .doReadSync();


            //获取对应的企业名称
            List<String> companyNames = list.stream().map(TTransportationCompanyStatistics::getCompanyName).filter(StrUtil::isNotEmpty).collect(Collectors.toList());
            //获取对应的区划名称
            List<String> districtNames = list.stream().map(TTransportationCompanyStatistics::getDistrictName).filter(StrUtil::isNotEmpty).collect(Collectors.toList());

            List<TTransportationCompanyInformation> informations = new ArrayList<>();
            List<TDistrict> districts = new ArrayList<>();
            if (!companyNames.isEmpty()) {
                informations = transportationCompanyInformationService.list(
                        new QueryWrapper<TTransportationCompanyInformation>()
                                .in("company_name", companyNames)
                );
            }

            if (!districtNames.isEmpty()) {
                districts = districtService.list(
                        new QueryWrapper<TDistrict>()
                                .in("district_name", districtNames)
                );
            }
            for (int i = 0; i < list.size(); i++) {
                TTransportationCompanyStatistics en = list.get(i);
                //校验数据是否异常
                if (en.getCarNumber() < 0) {
                    throw new HtException("数据异常！车辆数量不能小于0");
                }
                if (en.getRunNumber() < 0) {
                    throw new HtException("数据异常！每年趟次不能小于0");
                }
                if (en.getSeatNumber() < 0) {
                    throw new HtException("数据异常！车辆座位数不能小于0");
                }
                CommMethod.beanCreate(en);
                en.setYear(LocalDate.now().getYear());
                //设置企业编码
                TTransportationCompanyInformation information = informations.stream().filter(x -> StrUtil.equals(x.getCompanyName(), en.getCompanyName())).findFirst().orElse(null);
                if(information!=null){
                    en.setCompanyCode(information.getCompanyCode());
                }
                //设置区划编码
                TDistrict district = districts.stream().filter(x -> StrUtil.equals(x.getDistrictName(), en.getDistrictName())).findFirst().orElse(null);
                if(district!=null){
                    en.setDistrictCode(district.getDistrictCode());
                }
            }
            //批量保存
            this.saveBatch(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
